<?php
/*if ($_GET['id_ad'] == "0")
{
    include('insert_advert.php');
}
if ($_GET['id_ad'] == "1")
{
    include('edit_advert.php');
}*/
include('admin_advert.php');
/**
 * Created by PhpStorm.
 * User: tsa
 * Date: 17/12/2013
 * Time: 11:34 πμ
 */
if (!ISSET($con))
{
    include('auth_db.php');
}

if ($_POST['click'] == "1")
{
    $sql_q = 'UPDATE `category_action` SET `category_id`='.intval($_POST['category_id']).',`action_group_id`='.intval($_POST['action_group_id']).' WHERE `id`='.intval($_POST['id']);
    $result = mysqli_query($con, $sql_q);
    echo $sql_q;
}
if ($_POST['click'] == "2")
{
    $sql_q = 'DELETE FROM `category_action` WHERE `id`='.intval($_POST['id']);
    $result = mysqli_query($con, $sql_q);
    echo $sql_q;
}
$click = "0";
$_POST["click"] = "0";
$sql_q = 'SELECT * FROM `category_action` WHERE `id` = '.$_GET['id'];
$result = mysqli_query($con, $sql_q);
$rowa=mysqli_fetch_array($result);
echo $rowa['id'].','.$rowa['category_id'].','.$rowa['action_group_id'];
if ($_GET['id_ad'] == "2")
{
    $form = '<h4>Επεξεργασία αντιστοίχησης κατηγοριών-ενεργιών</h4>';
    $form = $form . '<form id="form_category_action_edit" method="post">';
    $form = $form . '<label> Κατηγορία</label>';
    $form = $form . '<select name="category_id"/>';
// $form = $form . '<option value="0" selected="selected"</option>';
    $sql_1 = "SELECT category.id,category.name FROM category group by name";
    $result1 = mysqli_query($con, $sql_1);
// echo $sql_1;
    while($row = mysqli_fetch_array($result1))
    {
        if ($rowa['category_id'] == $row['id'])
        {
        $form = $form . '<option selected value="'.$row['id'].'">'.$row['name'].'</option>';
        }
    //    else {
    //    $form = $form . '<option value="'.$row['id'].'">'.$row['name'].'</option>';
    //    }
    }
    $form = $form . '</select>';
    $form = $form . '<label>Ενέργια</label>';
    $form = $form . '<select name="action_group_id"/>';
    $sql_2 = "SELECT action_group.id,action_group.name FROM action_group group by name";
    $result2 = mysqli_query($con, $sql_2);
    while($row = mysqli_fetch_array($result2))
    {
        if ($rowa['action_group_id'] == $row['id'])
        {
            $form = $form . '<option selected value="'.$row['id'].'">'.$row['name'].'</option>';
        }
        else {
            $form = $form . '<option value="'.$row['id'].'">'.$row['name'].'</option>';
        }
    }
    $form = $form . '</select>';
    $form = $form . '<input name="id" type="hidden" value="'.$rowa['id'].'"/>';
    $form = $form . '<input name="click" type="hidden" value="1"/>';
    $form = $form . '<input type="submit" value="Ενημέρωση"/>';
    $form = $form . '</form>';
    echo $form;
}
elseif ($_GET['id_ad'] == "3") {
    $form = '<h4>Διαγραφή Εγγραφής:'.$rowa['id'].' '.$rowa['category_id'].'</h4>';
    $form = $form . '<form id="form_category_action_delete" method="post">';
    $form = $form . '<input name="id" type="hidden" value="'.$rowa['id'].'"/>';
    $form = $form . '<input name="click" type="hidden" value="2"/>';
    $form = $form . '<input type="submit" value="Διαγραφή"/>';
    $form = $form . '</form>';
    echo $form;
    $sql_2 = "SELECT category.id AS aid, category.name AS aname, category_action.id as bid, category_action.action_group_id AS aagid, action_group.id AS agid, action_group.name AS bname FROM  category, category_action, action_group WHERE category.id = category_action.category_id AND category_action.action_group_id = action_group.id GROUP BY category_action.id";
    $result = mysqli_query($con, $sql_2);
    echo '<table border="1">';
    echo '<tr><td>Id</td><td>Category id</td><td>Name</td><td>Action_Group_id</td><td>Action_Group_Name</td></tr>';
    while($row = mysqli_fetch_array($result))
    {
        echo '<tr><td>'.$row['bid'].'</td><td>'.$row['aid'].'</td><td>'.$row['aname'].'</td><td>'.$row['aagid'].'</td><td>'.$row['bname'].'</td><td>[ <a href="?page=admin_advert/category_action_edit&id_ad=2&id='.$row['bid'].'">Επεξεργασία</a> ]</td><td>[ <a href="?page=admin_advert/category_action_edit&id_ad=3&id='.$row['bid'].'">Διαγραφή</a> ]</td></tr>';
    }
    echo '</table>';
}
else
{
    echo "<br><h2>PAGE NOT FOUND</h2>";
}
?>